<template>
  <!-- 万秋配置添加修改页面 -->
  <el-dialog :visible.sync="visible" title="万秋配置" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="300px">
      <el-form-item prop="deptRate" label="VIP区复购分公司奖励比例">
        <el-input-number v-model="dataForm.deptRate" placeholder="请输入VIP区复购分公司奖励比例" :precision="2" :min="0.01" :max="1"></el-input-number>
      </el-form-item>
      <el-form-item prop="vipRate" label="VIP区复购直接父级奖励比例">
        <el-input-number v-model="dataForm.vipRate" placeholder="请输入VIP区复购直接父级奖励比例" :precision="2" :min="0.01" :max="1"></el-input-number>
      </el-form-item>
      <el-form-item prop="deptReward" label="普通区购买分公司固定奖励">
        <el-input-number v-model="dataForm.deptReward" placeholder="请输入普通区购买分公司固定奖励" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item prop="discountRate" label="VIP身份在VIP复购享受的折扣比例">
        <el-input-number v-model="dataForm.discountRate" placeholder="请输入VIP身份在VIP复购享受的折扣比例" :precision="2" :min="0.01" :max="1"></el-input-number>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      dialogVisible: false,
      dataForm: {
        id: '',                             //id
        deptRate: '',                       //VIP区复购分公司奖励比例
        vipRate: '',                        //VIP区复购直接父级奖励比例
        deptReward: '',                     //普通区购买分公司固定奖励
        discountRate: '',                   //VIP身份在VIP复购享受的折扣比例
      }
    }
  },
  computed: {
    dataRule() {
      return {
        deptRate: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        vipRate: [
        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
      ],
        deptReward: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        discountRate: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getInfo()
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/shopInfo/getWanQiuModel`).then(({data: res}) => {
        //console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http['post']('/api/shopInfo/saveWanQiuConfig', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
            }
          })
        }).catch(() => {})
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
